package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.BucketDriver;
import com.bocloud.cmp.driver.providers.ali.AliBucketDriver;
import com.bocloud.cmp.driver.providers.intf.BucketProvider;
import com.bocloud.cmp.driver.providers.jd.JdBucketDriver;
import com.bocloud.cmp.driver.providers.os.OSBucketDriver;
import com.bocloud.cmp.driver.providers.os.OSDriver.Version;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.common.model.BsmResult;

@Service("bucketProvider")
public class BucketProviderImpl implements BucketProvider {

	@Override
	public BsmResult create(String name, boolean share, String vendor, String region) {
		BsmResult result = null;
		BucketDriver driver = null;
		String url = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			url = this.buildUrl(butler, region);
			driver = new JdBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url, region);
			result = driver.create(name, share);
			if (result.isSuccess()) {
				result = driver.detail(name);
			}
			break;
		case ALIYUN:
			url = this.buildUrl(butler, region);
			driver = new AliBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url);
			result = driver.create(name, share);
			break;
		case OPENSTACK:
			driver = new OSBucketDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.create(name, share);
			break;
		default:
			result = new BsmResult(false, "BOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult exists(String name, String vendor, String region) {
		BsmResult result = null;
		BucketDriver driver = null;
		String url = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			url = this.buildUrl(butler, region);
			driver = new JdBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url, region);
			result = driver.exists(name);
			break;
		case ALIYUN:
			url = this.buildUrl(butler, region);
			driver = new AliBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url);
			result = driver.exists(name);
			break;
		case OPENSTACK:
			driver = new OSBucketDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.exists(name);
			break;
		default:
			result = new BsmResult(false, "BOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult remove(String name, String vendor, String region) {
		BsmResult result = null;
		BucketDriver driver = null;
		String url = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			url = this.buildUrl(butler, region);
			driver = new JdBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url, region);
			result = driver.remove(name);
			break;
		case ALIYUN:
			url = this.buildUrl(butler, region);
			driver = new AliBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url);
			result = driver.remove(name);
			break;
		case OPENSTACK:
			driver = new OSBucketDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.remove(name);
			break;
		default:
			result = new BsmResult(false, "BOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult detail(String name, String vendor, String region) {
		BsmResult result = null;
		BucketDriver driver = null;
		String url = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			url = this.buildUrl(butler, region);
			driver = new JdBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url, region);
			result = driver.remove(name);
			break;
		case ALIYUN:
			url = this.buildUrl(butler, region);
			driver = new AliBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url);
			result = driver.detail(name);
			break;
		case OPENSTACK:
			driver = new OSBucketDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.detail(name);
			break;
		default:
			result = new BsmResult(false, "BOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult list(String vendor, String region) {
		BucketDriver driver = null;
		String url = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			url = this.buildUrl(butler, region);
			driver = new JdBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url, region);
			break;
		case ALIYUN:
			url = this.buildUrl(butler, region);
			driver = new AliBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url);
			break;
		case OPENSTACK:
			driver = new OSBucketDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		}
		return driver.list();
	}

	private String buildUrl(Butler butler, String region) {
		String url = butler.getUrl();
		if (null != url) {
			VendorType type = VendorConverter.convert(butler.getCategory());
			switch (type) {
			case ALIYUN:
				url = url.replace("//", "//oss-@.");
				break;
			case JDYUN:
				break;
			default:
				break;
			}
			if (StringUtils.isEmpty(region) || region.equalsIgnoreCase("@")) {
				url = url.replaceAll("@", "");
			} else {
				url = url.replaceAll("@", region);
			}
		} else {
			url = "";
		}
		return url;
	}

	@Override
	public BsmResult acl(String bucket, boolean acl, String vendor, String region) {
		BsmResult result = null;
		BucketDriver driver = null;
		String url = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			url = this.buildUrl(butler, region);
			driver = new JdBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url, region);
			result = driver.acl(bucket, acl);
			break;
		case ALIYUN:
			url = this.buildUrl(butler, region);
			driver = new AliBucketDriver(butler.getAccessKey(), butler.getSecretKey(), url);
			result = driver.acl(bucket, acl);
			break;
		case OPENSTACK:
			driver = new OSBucketDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.acl(bucket, acl);
			break;
		default:
			result = new BsmResult(false, "BOT_SUPPORTED");
			break;
		}
		return result;
	}
}
